package jbp.controller;

import jbp.domain.version.AddVersionDto;
import jbp.domain.version.EditVersionDto;
import jbp.domain.version.VersionListDto;
import jbp.model.JbpPageResult;
import jbp.service.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/version")
public class VersionController extends JbpBaseController {
    @Autowired
    private VersionService versionService;

    @RequestMapping("/query")
    public Object Query(@RequestParam(value = "qparamstr") String qparamstr,
                        @RequestParam(value = "PageIndex") Integer PageIndex,
                        @RequestParam(value = "PageSize") Integer PageSize) {
        Map<String, Object> queryParams = GetQueryParam(qparamstr);
        if(PageIndex !=null && PageSize != null){
            Integer SkipCount = (PageIndex - 1) * PageSize;
            queryParams.put("SkipCount",SkipCount);
            queryParams.put("PageSize",PageSize);
            int TotalCount = versionService.Count(queryParams);
            List<VersionListDto> list = versionService.Query(queryParams);
            return new JbpPageResult<VersionListDto>(TotalCount,list);
        } else {
            return versionService.Query(queryParams);
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object Get(@PathVariable int id){
        return versionService.Query(id);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public boolean Post(@RequestBody AddVersionDto addDto){
        //System.out.println(addDto.getID());
        //System.out.println(addDto.getVersionNo());
        //System.out.println(addDto.getVersionTitle());
        return versionService.Add(addDto);
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    public boolean Put(@RequestBody EditVersionDto editDto){
        //System.out.println(editDto.getID());
        //System.out.println(editDto.getVersionNo());
        //System.out.println(editDto.getVersionTitle());
        return versionService.Update(editDto);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public boolean Delete(@PathVariable String id){
        return versionService.Delete(id);
    }
}
